// This class provides the implementation of a GuiInterface object,
// used to interface between the GUI and the backend code
//
// Created by River Warren
// Edited by Christopher Bowlsby, Raquel Rhodes
// Last updated on 11/23

#include "GuiInterface.h"
#include <iostream>
#include <string.h>
#include "Utility.h"
#include <algorithm>
#include "NewOrContinueBox.h"
#include "GetFile.h"


// constructor
GuiInterface::GuiInterface(Game *ptrToCurrentGame,int argc, char* argv[]) : Interface(ptrToCurrentGame) {
    this->argc = argc;
    this->argv = argv;
}


// this function sets up the player object for use in the game
void GuiInterface::setupPlayer() {

    string fileName;

    // permit player to load a game or start a new game
    QApplication app(argc, argv);
    NewOrContinueBox newOrContinue(new QWidget());
    int result = newOrContinue.showBox();

    // if the user desires to start a new game
    if(result == 0)
    {   // create player
        createPlayer playerSetup;
        playerSetup.exec();
        std::string errorMSG = "";


        // copy player stats into game pointer
        gamePtr->setupPlayer(playerSetup.getPlayerName(), playerSetup.getPlayerClass(), errorMSG);

        // will use this version later (game and a bunch of other header files
        // need to be modified first as they do not permit the user to generate stats for the player
        // at this time
        //gamePtr->setupPlayer(playerSetup.getPlayerName(), playerSetup.getPlayerClass(),
                //playerSetup.getPlayerStats());

        // now set index of chosen class so that other methods can access it
        indexOfClass = playerSetup.getIndex();

    }  // otherwise, load old game
    else {
        GetFile getFile;
        fileName = getFile.openFile();
        std::cout << fileName << std::endl;
        exit(0);
    }
}


// after construction of the player object, the main interface for the
// game is loaded in this function
void GuiInterface::loadInterface() {

    QApplication app(argc, argv);
    w = new Dungeons_and_Princesses_gui(indexOfClass);

    /****need to load contents into view before show()  *****/
    w->getInterfaceInstance(this);
    w->show();

    //std::string text = gamePtr->getCurrentRoom()->Base::getDesc();
    //QString roomDesc = QString::fromStdString(text);
    //this->showDescription();

    app.exec();

    // exit when complete
    exit(0);
}


void GuiInterface::showRoomInventory() {

    /*const vector<Listing> * roomInv = gamePtr->getCurrentRoom()->getManifest();

    if (roomInv->size() > 0) {
        //std::cout << "\n" << "In the room you see the following : " << endl;
        //for_each(roomInv->begin(), roomInv->end(), Utility::printListingOBject);

        //std::cout << endl;
    }*/
}


// this method prints out a description of the room onscreen
void GuiInterface::showDescription()
//QString GuiInterface::showDescription_gui()
{
    //std::string text = gamePtr->getCurrentRoom()->Base::getDesc();
    QString roomDesc = QString::fromStdString("text");
    //QString dir = this->showDirections();
    roomDesc.append("\n");
    //roomDesc.append(dir);

    w->setTextDesc(roomDesc);
    //return roomDesc;

}

void GuiInterface::showPlayerInventory() {

    const vector<Listing> * playerInv = gamePtr->getCurrentPlayer()->inventory.getManifest();
    QString textInvTemp, textInv;
    textInv = "";
    textInvTemp = "";

    if (playerInv->size() > 0) {
        //std::cout << "\n" << "You have in your inventory the following items : " << endl << endl;
      //  for_each(playerInv->begin(), playerInv->end(), Utility::printListingOBject);
        //std::cout << endl;
     /*   for (int i = 0; playerInv->size(); playerInv++)
        {
            std::string inv = playerInv[i].data();
            textInvTemp = QString::fromStdString(inv);
            textInv.append(textInvTemp);
            textInv.append(", ");
        }*/
        //update();
    }

    else {
        //update("You do not have anything in your inventory");
    }
}

void GuiInterface::showHelp(vector<std::string> *helpFile) {

    vector<std::string>::iterator helpLine;

    //std::cout << endl << endl;
    for (helpLine = helpFile->begin(); helpLine != helpFile->end(); helpLine++) {
       // std::cout << *helpLine << endl;
    }
    //update();
}


void GuiInterface::showDirections()
//QString GuiInterface::showDirections()
{
    QString roomDescTemp, roomDesc;
    std::string text = "";
    for (int directionNum = 0; directionNum < 4; directionNum++) {
        //text = gamePtr->getCurrentRoom()->getDirections()[directionNum].getDesc();
        roomDescTemp = QString::fromStdString(text);
        roomDesc.append("\n");
        roomDesc.append(roomDescTemp);
    }

    w->setTextDesc(roomDesc);
    w->update();

   //return roomDesc;
}

void GuiInterface::somethingHappened(std::string str)
{
  //gamePtr->executeCommand(str);
    QString temp = QString::fromStdString(str);

    w->setTextDesc(temp);

    // update gui
    w->update();

}


// included but not implemented at this time
void GuiInterface::enterBattle() {

}
void GuiInterface::showRoomInventory(const std::vector<Listing> *) {

}
void GuiInterface::showPlayerInventory(const std::vector<Listing> *) {

}
bool GuiInterface::saveGame() {
    return false;

}
int GuiInterface::loadGame() {
    return 0;
}
void GuiInterface::printErrorMsg(string) {

}
